/*
 * Enemy.cpp
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "Enemy.h"
#include "Images.h"
#include "Ship.h"

#define VERTICAL_OFFSET 3.5f
#define DRIFTING_STEP   0.5f
#define DRIFTING_BORDER 4.5f

Enemy::Enemy(const SDL_Rect& area) :
    _area(area),
    _formation(area),
    _drift_counter(40, Counter::LOOP),
    _drift_shift(0, Counter::OSCILLATE),
    _attack_counter(120, Counter::LOOP),
    _attack_side(1)
{
    _formation.load("  ssssss\n"
                    " ssssssss\n"
                    "ssssssssss\n"
                    "ssssssssss\n"
                    "ssssssssss");

    _formation.move((_area.w - _formation.width()) / 2,
                     VERTICAL_OFFSET * Images::get(Images::SOLDIER)->h);

    int drift_border = DRIFTING_BORDER * Images::get(Images::SOLDIER)->w;
    _drift_shift.set_period(_area.w - _formation.width() - 2 * drift_border); 
    _drift_shift.set_step(DRIFTING_STEP * Images::get(Images::SOLDIER)->w);
    _drift_shift.set(_drift_shift.period() / 2);
}

void Enemy::drift()
{
    _drift_counter.tick();

    if (_drift_counter.hit())
    {
        _drift_shift.tick();
        _formation.move(_drift_shift.delta(), 0);
    }
}

void Enemy::attack(Ship* ship)
{
    _attack_counter.tick();

    if (_attack_counter.hit())
    {
        if (_attackers.size() < 2)
            send_attacker(ship);
        else
            _attack_counter.restart();
    }
}

void Enemy::send_attacker(Ship* ship)
{
    int x = _attack_side < 0 ? _formation.cols() - 1 : 0;
    for (;x < _formation.cols() && x >= 0; x += _attack_side)
        for (int y = 0; y < _formation.rows(); ++y)
        {
            Soldier* soldier = _formation.get(x, y);
            if (soldier)
            {
                _formation.clean(x, y);
                soldier->attack(ship);
                _attackers.push_back(soldier);
                _attack_side = -_attack_side;
                return;
            }
        }
}

bool Enemy::update(void* arg)
{
    Ship* ship = reinterpret_cast<Ship*>(arg);
    drift();
    _attackers.update(ship);
    _formation.update(ship);
    attack(ship);
    return _formation.population() > 0 || _attackers.size() > 0;
}

void Enemy::erase(SDL_Surface* screen, SDL_Surface* background)
{
    _formation.erase(screen, background);
    _attackers.erase(screen, background);
}

void Enemy::blit(SDL_Surface* screen)
{
    _formation.blit(screen);
    _attackers.blit(screen);
}

